package org.xilin
import org.xilin.Family
import org.xilin.Semester
import org.xilin.Course

class ClassRegisterController {
    def authenticateService
    def semesterService
    def classRegisterService
    def index = { 
    }

    def addBook = {
        Course courseInstance = Course.get(params.courseId)
        if(courseInstance?.book) {
            render (template:'addBook', model:[courseInstance:courseInstance])
        } else {
            chain addCourse
        }
    }

    def addCourse = {
        def list = semesterService.getListBySemester(Course)
        //        def familyInstance = classRegisterService.getUserFamily()
        def studentInstance = Student.get(params.studentId)

        Course courseInstance = Course.get(params.courseId)

        if(!classRegisterService.addClass(courseInstance, studentInstance)) {
            flash.message = "Time conflict!"
        }
        redirect action:makeSelection, params:params
    }

    def dropCourse = {
        def studentInstance = Student.get(params.studentId)
        Course courseInstance = Course.get(params.courseId)
        classRegisterService.dropClass(studentInstance, courseInstance)
        def list = semesterService.getListBySemester(Course)
        def user = authenticateService.userDomain()
        def familyInstance = Family.findByUserId(user.username)
        render (template:'makeSelection', model:[courseInstanceList: list, courseInstanceTotal: list.size(), familyInstance : familyInstance, studentInstance:studentInstance, params:params])
    }

    def uncheckBook = {
        TheClass c = TheClass.get(params.classId)
        c.setBookIncluded(false)
        c.save(flush:true)
        def studentInstance = Student.get(params.studentId)
        studentInstance.save(flush:true)
        def list = semesterService.getListBySemester(Course)
        def user = authenticateService.userDomain()
        def familyInstance = Family.findByUserId(user.username)
        
        render (template:'makeSelection', model:[courseInstanceList: list, courseInstanceTotal: list.size(), familyInstance : familyInstance, studentInstance: studentInstance, params:params])
    }

    def makeSelection = {
        def list = semesterService.getListBySemester(Course)
        def familyInstance = classRegisterService.getUserFamily()

        if (!params.studentId) {
            def studentList = familyInstance.students.toList().sort()
            params.studentId = studentList.first().id
            //          params.putAt("studentId", studentList.first().id)
        }

        def studentInstance = Student.get(params.studentId)
        studentInstance.classes.each {
            println it
        }
        render (template:'makeSelection', model:[courseInstanceList: list, courseInstanceTotal: list.size(), studentInstance: studentInstance, params:params])
    }


    def showBySemester = {
        flash.message = "semester selected: ${params.semester}"
        def semester = Semester.findByTerm("${params.semester}")
        def courseList = Course.findAllBySemester(semester)
        render (template:'showBySemester', model:[courseInstanceList: courseList])
    }

    def bookOption = {
        TheClass c = TheClass.get(params.classId)
        if(params.bookChecked == "on") {  // unSelect book
            c.setBookIncluded(false)
            c.course.book.reserve--
        } else {
            c.setBookIncluded(true)
            c.course.book.reserve++
        }
        c.course.book.save(flush:true)
        c.save(flush:true)
        def studentInstance = Student.get(params.studentId)
        studentInstance.save(flush:true)
        def list = semesterService.getListBySemester(Course)
        def user = authenticateService.userDomain()
        def familyInstance = Family.findByUserId(user.username)
        render (template:'makeSelection', model:[courseInstanceList: list, courseInstanceTotal: list.size(), familyInstance : familyInstance, studentInstance: studentInstance, params:params])

    }

    
    def showRegisteredClass = {
        def user = authenticateService.userDomain()
        def familyInstance = Family.findByUserId(user.username)
        render (template:'showRegisteredClass', model:[familyInstance: familyInstance])
    }

    def selectStudent = {
        if (params.redirectAction) {
            redirect(controller: params.redirectController, action: params.redirectAction, params:params)
        }
        else {
            render "Student changed"
        }
    }

}
